<?php

	/**
	* @author zhouguoyan
	* @desc
	**/	
	
class LoginInfoController extends AppController {
	var $name='LoginInfo';
    var $uses = array('Company','SysParams','CompanyArea','CompanyAccount','CompanyCategory');
	var $access = array ('detail'=>array('role' => array('Admin','Staff')),
					     'edit'=>array('role' => array('Admin','Staff')),
					     'edit_confirm'=>array('role' => array('Admin','Staff')),
					     'edit_save'=>array('role' => array('Admin','Staff'))
					      );
	/**
	* @author zhouguoyan
	* @desc 进入详细页面
	**/	
		
	function detail(){
    	if(empty($this->rdAuth->id)){
    		$this->redirect('/Auth/login');
    	}
    	$company_id = $this->rdAuth->company_id;
		$company=$this->Company->find('"Company".id='.$company_id);		
		$company['Company']['company_area']=$this->processData1($company,'CompanyArea','company_area_id');
		$company['Company']['company_tema']=$this->processData1($company,'CompanyCategory','category_id');
		if(!empty($company['Company']['logo_name'])){
			$company['Company']['logo_path']='/'.$this->Prop->getValue('config.image.dir').'/'.floor($company_id/1000).'/'.$company_id.'/'.$company['Company']['logo_name'];
		}
		$this->set('company',$company);
		
	}
	
	function getyears($internal=60) {
	    $years=array();
	    $currentTimestamp = strtotime('now');
	    for($i=0;$i<$internal;$i++){
	        $years[date('Y',strtotime("-$i year",$currentTimestamp))]=date('Y',strtotime("-$i year",$currentTimestamp));
	    }
	    return $years;
	}
	
	function getmonth(){
		$month=array();
		for($i=1;$i<=12;$i++){
			if($i<10){
				$month['0'.$i]='0'.$i;
			}else{
				$month[$i]=$i;
			}
			
		}
		return $month;
	}
	
	/**
	* @author zhouguoyan
	* @desc 进入编辑页面
	**/	
	function edit($company_id,$editflg=null){
    	if(empty($this->rdAuth->id)){
    		$this->redirect('/Auth/login');
    	}
    	$this->checkAccess1($company_id,$this->rdAuth->company_id);	
	    if(empty($editflg)){
	    	$this->Session->del('editCom');
	    }
		$this->set('areas',$this->SysParams->generateList("\"SysParams\".kbn='area_id' and \"SysParams\".deleted is null"));
		$this->set('temas',$this->SysParams->generateList("\"SysParams\".kbn='tema_id' and \"SysParams\".deleted is null"));
		$this->set('companyid',$company_id);
		$year = $this->getyears();
		$month = $this->getmonth();
		$this->set('year',$year);
		$this->set('month',$month);
		$editForm=$this->Session->read('editCom');
		if(!empty($editForm)&&!empty($editflg)){
			$this->data=$editForm;
			$this->render('edit');
			exit;
		}
	    $com=$this->Company->find('"Company".id='.$company_id);
	    $year = split("-",$com['Company']['company_created']);
	    
	    $com['Company']['company_created_year']=$year['0'];
	    if(isset($year['1'])){
		    $com['Company']['company_created_month']=$year['1'];
	    }
	    
		$this->data=$com;
	 	
	 	$this->data['Company']['id']=$company_id;
	 	$this->data['Company']['email_confirm']=$com['Company']['email'];
	 	$this->data['Company']['incept_email_confirm']=$com['Company']['incept_email'];

	}


	function processData1($data,$key,$kbn){
    	if(empty($this->rdAuth->id)){
    		$this->redirect('/Auth/login');
    	}
		if(!isset($data[$key])){
			return '';
		}
		$arr = array();
		foreach($data[$key] as $d){
			$sysdata=null;
			if($key=='CompanyArea'){
				$sysdata=$this->SysParams->find("\"SysParams\".kbn='area_id' and \"SysParams\".id=".$d[$kbn]);
			}elseif($key=='CompanyCategory'){
				$sysdata=$this->SysParams->find("\"SysParams\".kbn='tema_id' and \"SysParams\".id=".$d[$kbn]);
			}else{
			    $sysdata=$this->SysParams->find('"SysParams".kbn='.'\''.$kbn.'\''.' and "SysParams".id='.$d[$kbn]);
			}
			$arr[]=$sysdata['SysParams']['name'];
		}		
		return  implode(',',$arr);	
	}	
	/**
	* @author zhouguoyan
	* @desc 验证
	**/	
	function edit_confirm(){
    	if(empty($this->rdAuth->id)){
    		$this->redirect('/Auth/login');
    	}
		$relateData=$this->params['form'];
		$company=$relateData['data'];
		$message = $this->Company->Company_Valid($this->Valid,$this->data);
//-----20081126 update

		//Email重复验证
		$email = addslashes($this->data['Company']['email']);
		$company_id=$this->data['Company']['id'];
		$condition = "\"Company\".deleted is null AND " .
					 "\"Company\".id<>$company_id AND " .
					 "\"Company\".email = '$email' AND " .
					 "\"Company\".company_type_id = 1"; //1:人材紹介会社　２：一般企業
		$check_mail_repeat = $this->Company->find($condition);
		if(!empty($check_mail_repeat)){
			$message .= $this->Valid->tagErrorMsg('exist','Company/email');
		}
		
//-----20081126 update
		if($message){
		$this->set('message', $message);
		$this->set('areas',$this->SysParams->generateList("\"SysParams\".kbn='area_id' and \"SysParams\".deleted is null"));
		$this->set('temas',$this->SysParams->generateList("\"SysParams\".kbn='tema_id' and \"SysParams\".deleted is null"));	
		$year = $this->getyears();
		$month = $this->getmonth();
		$this->set('year',$year);
		$this->set('month',$month);
		$this->render('edit');
		exit;
	    }
		$company['Company']['company_created']=$company['Company']['company_created_year'].'-'.$company['Company']['company_created_month'];		
		$this->set('company',$company);				              
        $this->Session->write('editCom',$company);
 
	}	
	
    function processData($data,$kbn){
		if(empty($this->rdAuth->id)){
			$this->redirect('/Auth/login');
		}	
		if(empty($data)){
			return '';
		}
		$arr = array();
		foreach($data as $id){
			$sysdata=null;
			if($kbn=='company_area_id'){
				$sysdata=$this->SysParams->find("\"SysParams\".kbn='area_id' and \"SysParams\".id=".$id);
			}elseif($kbn=='category_id'){
				$sysdata=$this->SysParams->find("\"SysParams\".kbn='tema_id' and \"SysParams\".id=".$id);
			}else{
			    $sysdata=$this->SysParams->find('\"SysParams\".kbn='.'\''.$kbn.'\''.' and \"SysParams\".id='.$id);
			}
			$arr[]=$sysdata['SysParams']['name'];
		}		
		return  implode(',',$arr);	
	}

	/**
	* @author zhouguoyan
	* @desc 把数据添加数据库
	**/	
	function edit_save(){
    	if(empty($this->rdAuth->id)){
    		$this->redirect('/Auth/login');
    	}
		$relateData=$this->Session->read('editCom');
		$company_id=$relateData['Company']['id'];
		$relateData['Company']['update_id']=4;
		if($this->Company->save($relateData['Company'])){			
	 		$this->Session->del('editCom');
		}
		$this->redirect('LoginInfo/finish');		
	}
	function finish(){}



}
?>
